<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="审核结果" prop="checkStatus">
        <el-select v-model="queryParams.checkStatus" placeholder="请选择审核结果" style="width: 160px" clearable>
          <el-option
            v-for="dict in flow_check_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请时间" style="width: 308px">
        <el-date-picker
          v-model="flowTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Check"
          :disabled="multiple"
          @click="handlePass"
          v-hasPermi="['asset:flow:check']"
        >批量通过</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Close"
          :disabled="multiple"
          @click="handleUnPass"
          v-hasPermi="['asset:flow:check']"
        >批量不通过</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="flowList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工单类别" align="center" prop="flow.typeName" />
      <el-table-column label="单号" align="center" prop="flow.num" />
      <el-table-column label="申请部门" align="center" prop="flow.deptName" />
      <el-table-column label="申请人" align="center" prop="flow.userName" />
      <el-table-column label="资产编码" align="center" prop="flow.assetNum" />
      <el-table-column label="资产分类" align="center" prop="flow.cateName" />
      <el-table-column label="申请数量" align="center" prop="flow.amount" />
      <el-table-column label="申请原由" align="center" prop="flow.reason" />
      <el-table-column label="申请说明" align="center" prop="flow.content" />
      <el-table-column label="申请时间" align="center" prop="flow.applyTime" width="180" />
      <el-table-column label="审核时间" align="center" prop="checkTime" width="180" v-if="queryParams.checkStatus != '0'"/>
      <el-table-column label="审核说明" align="center" prop="checkComment" width="180" v-if="queryParams.checkStatus != '0'"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="queryParams.checkStatus == '0'">
        <template #default="scope">
          <el-button v-if="scope.row.checkStatus == '0'" link type="primary" icon="Check" @click="handleCheck(scope.row)">审核</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <check-dialog :flowData="flowData" @close="cancelSubmit" @submit="doSubmit"/>
  </div>
</template>

<script setup name="Flow">
import { listMyCheck, batchCheck } from "@/api/asset/flowCheck";
import CheckDialog from '@/components/FlowDialog/CheckDialog'

const { proxy } = getCurrentInstance();
const { asset_flow_type, flow_check_status } = proxy.useDict('asset_flow_type', 'flow_check_status');

const flowList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const flowTime = ref([]);
const flowData = ref(null);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderByColumn: 't.id',
    isAsc: 'DESC',
    type: null,
    num: null,
    checkStatus: '0'
  },
  rules: {
    type: [
      { required: true, message: "工单类别不能为空", trigger: "change" }
    ],
    num: [
      { required: true, message: "单号不能为空", trigger: "blur" }
    ],
    cateId: [
      { required: true, message: "资产分类不能为空", trigger: "blur" }
    ],
    deptId: [
      { required: true, message: "申请部门不能为空", trigger: "blur" }
    ],
    userId: [
      { required: true, message: "申请人不能为空", trigger: "blur" }
    ],
    amount: [
      { required: true, message: "申请数量不能为空", trigger: "blur" }
    ],
    reason: [
      { required: true, message: "申请原由不能为空", trigger: "blur" }
    ],
    applyTime: [
      { required: true, message: "申请时间不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询资产工单列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != flowTime && '' != flowTime) {
    queryParams.value.params["beginFlowTime"] = flowTime.value[0];
    queryParams.value.params["endFlowTime"] = flowTime.value[1];
  }
  listMyCheck(queryParams.value).then(response => {
    flowList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    type: null,
    num: null,
    cateId: null,
    deptId: null,
    userId: null,
    amount: null,
    reason: null,
    content: null,
    checkStatus: null,
    flowStatus: null,
    applyTime: null
  };
  proxy.resetForm("flowRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  flowTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 审核按钮操作 */
function handleCheck(row) {
  flowData.value = row.flow;
  flowData.value.checkId = row.id;
}

function cancelSubmit(){
  flowData.value = null;
}
function doSubmit(){
  flowData.value = null;
  getList();
}

/** 删除按钮操作 */
function handlePass() {
  proxy.$modal.confirm('是否确认将选定的资产工单标记为审核通过状态？').then(function() {
    doBatchCheck("1");
  });
}
function handleUnPass() {
  proxy.$modal.confirm('是否确认将选定的资产工单标记为审核不通过状态？').then(function() {
    doBatchCheck("2");
  });
}

function doBatchCheck(checkStatus){
  batchCheck({ids:ids.value, checkStatus:checkStatus}).then(() => {
    getList();
    proxy.$modal.msgSuccess("审核成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('asset/flow/export', {
    ...queryParams.value
  }, `flow_${new Date().getTime()}.xlsx`)
}

getList();
</script>
